public class HashBuck {

    static class Node {
        public int key;
        public int value;
        public Node next;

        public Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
    }

    public Node[] array;
    public int UseSize;
    public static final double DOUBLE_KEY = 0.75;

    public HashBuck() {
        this.array = new Node[8];
    }

    public void put(int key,int value) {
        Node node = new Node(key,value);

        int index = key % array.length;
        Node cur = array[index];

        while(cur != null) {
            if(cur.key == key) {
                cur.value = node.value;
                return;
            }
            cur = cur.next;
        }

        node.next = array[index];
        array[index] = node;
        UseSize++;
        double x = loadFactor();
        if(x >= DOUBLE_KEY) {
            resize();
        }

    }

    private void resize() {
        Node[] tmpAry = new Node[array.length*2];


        for (int i = 0; i < array.length; i++) {
            Node cur = array[i];

            while(cur != null) {
                Node prev = cur.next;
                int index = cur.key % array.length;
                cur.next = tmpAry[index];
                tmpAry[index] = cur;
                cur = prev;
            }
        }

        array = tmpAry;

    }

    private double loadFactor() {
        int a = array.length;
        int b = UseSize;
        return b * 1.0 / a;
    }

    public int get(int key) {

        for (int i = 0; i < array.length; i++) {
            Node cur = array[i];

            while(cur != null) {
                if(cur.key == key) {
                    return cur.value;
                }
                cur = cur.next;
            }
        }

        return -1;
    }
}
